#pragma once

#include "Common.hpp"

class InetAddr
{
public:
    InetAddr()
    {}
    InetAddr(struct sockaddr_in &addr)
    {
        SetAddr(addr);
    }

    InetAddr(const std::string &ip, uint16_t port):_ip(ip), _port(port)
    {
        memset(&_addr, 0 ,sizeof(_addr));
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(_port);
        inet_pton(AF_INET, _ip.c_str(), &_addr.sin_addr);
    }

    InetAddr(uint16_t port):_port(port)
    {
        memset(&_addr, 0 ,sizeof(_addr));
        _addr.sin_family = AF_INET;
        _addr.sin_addr.s_addr = INADDR_ANY;
        _addr.sin_port = htons(_port);
    }

    void SetAddr(struct sockaddr_in &addr)
    {
        _addr = addr;
        _port = ntohs(_addr.sin_port);
        char ipbuffer[64];
        inet_ntop(AF_INET, &_addr.sin_addr, ipbuffer, sizeof(addr));
        _ip = ipbuffer;
    }

    std::string ip() {return _ip;}
    uint16_t port() {return _port;}
    struct sockaddr_in &NetAddr() {return _addr;};
    size_t NetAddrLen() {return sizeof(_addr);};
    struct sockaddr* NetAddrPtr()
    {
        return CONV(_addr);
    }
    std::string StringAddr()
    {
        return _ip + " : " + std::to_string(_port);
    }

    bool operator==(const InetAddr &addr)
    {
        if(_ip == addr._ip && _port == addr._port)
            return true;
        return false;
    }

    ~InetAddr()
    {

    }
private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};